<?php
namespace app\api\controller;

use think\Session;
use think\validate;
/**
* 
*/
class Base extends validate
{
	// public function _initialize()
	// {
	// 	$id = session('User.id');
	// 	# 检查用户是否登录和存在
	// 	if(!$id)
	// 	{
	// 		return $this->json(0,'用户未登录',array());
	// 	}
	// 	$userInfo = $this->user_exist($id);
	// 	if(!$userInfo)
	// 	{
	// 		return $this->json(0,'用户不存在',array());
	// 	}
	// }
	public function json($code,$info = '',$data = array())
    {
            if (!is_numeric($code)){
                return '错误';
            }
             $result = array(
                'code' => $code,
                'info' => $info,
                'data' => $data
        );
        echo json_encode($result);
        exit;
    }
    /**
     * 
     * 检验用户是否存在
     * @param  [int] $id [用户ID]
     * @return [bool]     [description]
     */
    public function user_exist($id)
    {
    	# 查询用户信息
    	$userInfo = db('users')->where('id',$id)->find();
    	if(!empty($userInfo))
    	{
    		return true;
    	}else{
    		return false;
    	}
    }

    /*

	时间转换函数

	*/

	public function transTime($ustime)
	{

		$ytime = date("Y-m-d H:i",$ustime);

		$rtime = date("n月j日 H:i",$ustime);

		$htime = date("H:i",$ustime);

		$time = $ustime - time();

		$todaytime = strtotime("today");

		$time1 = time() - $todaytime;

		# 获取明年时间戳
		$nextYear = strtotime("+1 year");

		if($time < 60){
			$str = '刚刚';

		}else if($time < 60 * 60){

			$min = floor($time/60);

			$str = $min.'分钟前';

		}else if($time < $time1){

			$str = '今天'.$htime;

		}else if($ustime > $nextYear){
			$str = $ytime;
		}else{

			$str = $rtime;

		}
		return $str;

	}
}
?>